const UglifyPlugin = require('uglifyjs-webpack-plugin')
const path = require('path')

function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  devServer: {
    port: 8899,
    https: false
  },
  publicPath: './',
  assetsDir: './',
  outputDir: './dist',
  indexPath: 'index.html',
  productionSourceMap: true,
  chainWebpack: (config) => {
    config.module.rule('images').test(/\.(jpg|jpeg|png|gif|hdr|tif)$/).use('url-loader').loader('url-loader')
      .options({limit: 2, outputPath: './images', name: '[name].[hash:5].[ext]'}).end()
    config.module.rule('models').test(/\.(fbx|glb|gltf|obj|drc|zip)$/i).use('url-loader').loader('url-loader')
      .options({limit: 5, outputPath: './models', name: '[name].[ext]'}).end()
  },
  configureWebpack: (config) => {
    if (process.env.NODE_ENV === 'prod') {
      // 为生产环境修改配置
      config.mode = 'production'
      // 将每个依赖包打包成单独的js文件
      let optimization = {
        minimizer: [new UglifyPlugin({
          cache: true,
          parallel: true,
          uglifyOptions: {
            warnings: false,
            compress: {
              // eslint-disable-next-line camelcase
              drop_console: true,
              // eslint-disable-next-line camelcase
              drop_debugger: true,
              // eslint-disable-next-line camelcase
              pure_funcs: ['console.log']
            }
          }
        })]
      }
      Object.assign(config, {
        optimization
      })
    } else {
      // 为开发环境修改配置
      config.mode = 'development'
    }
  }
}
